package com.pingan.agentbank.aaos;

import com.pingan.agentbank.aaos.pojo.Config;
import com.pingan.agentbank.aaos.service.BusinessService;
import com.pingan.agentbank.aaos.service.DataInDBService;
import com.pingan.agentbank.aaos.task.*;
import com.pingan.agentbank.aaos.util.ConfigUtil;
import com.pingan.vframe.Controller;
import com.pingan.vframe.SystemConfig;
import com.pingan.vframe.beans.Injector;
import com.pingan.vframe.db.DBName;
import com.pingan.vframe.lang.Inject;
import com.pingan.vframe.logging.Log;
import com.pingan.vframe.logging.LogFactory;
import com.pingan.vframe.threadpool.ThreadPool;
import com.pingan.vframe.util.ClassUtil;

import java.io.File;
import java.lang.annotation.Annotation;

public class Start {

    private final Log log = LogFactory.getLog();
    @Inject("AAOS") ThreadPool tp;
    @Inject Injector injector;


    public static void main(String[] args) {

        System.setProperty("aaosAutoStart","false");

        Injector injector = Controller.getInstance().getInjector();
        Start vs = injector.getBean(Start.class);
        vs.startup();

        try {
            Thread.sleep(Long.MAX_VALUE);
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    public void startup() {

        log.info("[ AAOS ]******************AAOS STARTUP*****************");
        String orgCodeStr = SystemConfig.getInstance().getString("com.aaos.orgcodes");
        String[] orgCodes = orgCodeStr.split(",");
        if (orgCodes.length == 0){
            return;
        }
        for (String code:orgCodes){
            startupDetail(code);
        }
    }

    public void startupDetail(String orgCode){

        Config config = ConfigUtil.loadConfig(orgCode);

        log.info("[ AAOS ] "+config.getBank() + " AAOS system starup ......");

        //如果没有fields文件夹则创建
        File file = new File(config.getFieldsDir());
        if (!file.isDirectory()){
            file.mkdir();
        }


        String dbName = config.getDbName();

        Annotation inDataBaseTaskDbname = ClassUtil.getAnnotation(InDataBaseTask.class, DBName.class);
        try {
            ClassUtil.changeAnnotationValue(inDataBaseTaskDbname,"value",dbName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        InDataBaseTask inDataBaseTask = injector.getBean(InDataBaseTask.class);

        Annotation businessTaskDbname = ClassUtil.getAnnotation(BusinessTask.class, DBName.class);
        try {
            ClassUtil.changeAnnotationValue(businessTaskDbname,"value",dbName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        BusinessTask businessTask = injector.getBean(BusinessTask.class);


        Annotation dbCleanTaskDbname = ClassUtil.getAnnotation(DBCleanTask.class, DBName.class);
        try {
            ClassUtil.changeAnnotationValue(dbCleanTaskDbname,"value",dbName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        DBCleanTask dbCleanTask = injector.getBean(DBCleanTask.class);

        BackUpCleanTask backCleanTask = injector.getBean(BackUpCleanTask.class);


        inDataBaseTask.setConfig(config);
        businessTask.setConfig(config);
        backCleanTask.setConfig(config);
        dbCleanTask.setConfig(config);




        //未完成的入库 和 任务 继续完成

        long minute = 60*1000L;
        long hour = 60*60*1000L;
        long day = 24*hour;

        //入库
        //获取InDataBaseTask
        log.info("[ AAOS ] "+config.getBank() + " file in database task start");
        long inDbInterval = config.getIndbInterval()*minute;
        tp.executeAfter(inDataBaseTask,1000L,inDbInterval);

       //备份文件夹清理
        log.info("[ AAOS ] "+config.getBank() + " backup file clean task start");
        long backUpInterval = config.getBackUpCleanInterval()*day;
        tp.executeAfter(backCleanTask,1000L,backUpInterval);

        //数据库清理
        log.info("[ AAOS ] "+config.getBank() + " db record clean task start");
        long dbInterval = config.getDbCleanInterval()*day;
        tp.executeAfter(dbCleanTask,1000L,dbInterval);

        //任务执行
        log.info("[ AAOS ] "+config.getBank() + " business task start");
        long exeInterval = config.getTaskInterval()*minute;
        tp.executeAfter(businessTask,1000L,exeInterval);
    }
}
